home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 May: Tool Chest / Developer CD Series Tool Chest (Apple Computer)(May 1999).iso / What's New? / Technical Documentaion / Macintosh Technotes and Q&As / technotes / tn / tn1137.hqx / InterruptDisableLib1.0b2 / InterruptDisableLib.c next >
Encoding:
Text File  |  1998-03-28  |  3.1 KB  |  119 lines

  1. /*
  2.     File:        InterruptDisableLib.c
  3.  
  4.     Contains:    Routines for disabling interrupts on 68K and PPC.
  5.  
  6.     Written by:    Quinn "The Eskimo!"
  7.  
  8.     Copyright:    © 1998 by Apple Computer, Inc., all rights reserved.
  9.  
  10.     Change History (most recent first):
  11.  
  12.     You may incorporate this sample code into your applications without
  13.     restriction, though the sample code has been provided "AS IS" and the
  14.     responsibility for its operation is 100% yours.  However, what you are
  15.     not permitted to do is to redistribute the source as "DSC Sample Code"
  16.     after having made changes. If you're going to re-distribute the source,
  17.     we require that you make it clear in the source that the code was
  18.     descended from Apple Sample Code, but that you've made changes.
  19. */
  20.  
  21. /////////////////////////////////////////////////////////////////////
  22. // Our prototypes
  23.  
  24. #include <MixedMode.h>
  25.  
  26. /////////////////////////////////////////////////////////////////////
  27. // Our prototypes
  28.  
  29. #include "InterruptDisableLib.h"
  30.  
  31. /////////////////////////////////////////////////////////////////////
  32. // Low-Level, Architecture-Specific, Subroutines
  33.  
  34. #if GENERATINGPOWERPC
  35.  
  36.     // PowerPC Specific Code
  37.     
  38.     // On PPC, we use MixedMode to handle moving the PPC parameters
  39.     // into the right 68K registers and back again.  This make our
  40.     // 68K very easy to write.
  41.     
  42.     enum {
  43.         kGetSRProcInfo = kRegisterBased
  44.                 | RESULT_SIZE(SIZE_CODE(sizeof(UInt16)))
  45.                 | REGISTER_RESULT_LOCATION(kRegisterD0),
  46.         kSetSRProcInfo = kRegisterBased
  47.                 | RESULT_SIZE(0)
  48.                 | REGISTER_ROUTINE_PARAMETER(1, kRegisterD0, SIZE_CODE(sizeof(UInt16)))
  49.     };
  50.     
  51.     // We define the 68K as a statically initialised data structure.
  52.     // The use of MixedMode to call these routines makes the routines
  53.     // themselves very simple.
  54.  
  55.     static UInt16 gGetSR[] = {
  56.         0x40c0,        // move sr,d0
  57.         0x4e75        // rts
  58.     };
  59.  
  60.     static UInt16 gSetSR[] = {
  61.         0x46c0,        // move d0,sr
  62.         0x4e75        // rts
  63.     };
  64.  
  65.     static UInt16 GetSR(void)
  66.         // Returns the current value of the SR, interrupt mask
  67.         // and all!  This routine uses MixedMode to call the gGetSR data
  68.         // structure as if it was 68K code (which it is!).
  69.         
  70.     {
  71.         return CallUniversalProc( (UniversalProcPtr) &gGetSR, kGetSRProcInfo);
  72.     }
  73.  
  74.     static void SetSR(UInt16 newSR)
  75.         // Returns the value of the SR, including the interrupt mask and all
  76.         // the flag bits.  This routine uses MixedMode to call the gGetSR data
  77.         // structure as if it was 68K code (which it is!).
  78.     {
  79.         CallUniversalProc( (UniversalProcPtr) &gSetSR, kSetSRProcInfo, newSR);
  80.     }
  81.  
  82. #else
  83.  
  84.     // Classic 68K and CFM-68K Specific Code
  85.  
  86.     // On classic 68K (and CFM-68K) we can simply access the
  87.     // 68K SR register using some inline procedures.
  88.     
  89.     static UInt16 GetSR(void) = {
  90.         0x40c0        // move sr,d0
  91.     };
  92.  
  93.     #pragma parameter SetSR(__D0)
  94.     static void SetSR(UInt16 newSR) = {
  95.         0x46c0        // move d0,sr
  96.     };
  97.  
  98. #endif
  99.  
  100. /////////////////////////////////////////////////////////////////////
  101. // High-Level Entry Points
  102.  
  103. extern pascal UInt16 GetInterruptMask(void)
  104.     // See comment in header file.
  105. {
  106.     return (GetSR() >> 8) & 7;
  107. }
  108.  
  109. extern pascal UInt16 SetInterruptMask(UInt16 newMask)
  110.     // See comment in header file.
  111. {
  112.     UInt16 currentSR;
  113.     
  114.     currentSR = GetSR();
  115.     SetSR( (currentSR & 0xF8FF) | (newMask << 8) );
  116.     
  117.     return (currentSR >> 8) & 7;
  118. }
  119.